import axios from 'axios'
// import type { UserInfo } from '~/store/useUserStore'
// import { KEY_USER_ID } from '~/store/useUserStore'

// 创建 axios 实例
const serverApi = axios.create({
  baseURL: import.meta.env.VITE_API_URL,
  timeout: 10000,
})

// 拦截器
// serverApi.interceptors.request.use((config) => {
//   config = config || {}
//   // pinia
//   try {
//     const user = JSON.parse(localStorage.getItem(KEY_USER_ID) || '') as UserInfo
//     if (user.token)
//       config.headers!.Authorization = `Bearer ${user.token}`
//   }
//   catch (e) {}
//   return config
// })

// 响应拦截器
serverApi.interceptors.response.use(
  (resp) => {
    const { code, msg } = resp.data || {}
    if (code !== OK_CODE)
      return Promise.reject(msg)

    if (code === NO_PERMISSION) {
      useRouter().push({ name: 'Login' }).then()
      return Promise.reject(msg)
    }
    return Promise.resolve(resp)
  },
  // (error) => {
  //   return Promise.reject(error)
  // },
)

export default serverApi
